
export type EventType =
  "dragStart" |
  "drag" |
  "dragEnd" |
  "click";

export type EventHandler = (evt: BaseEvent) => void  

export default interface EventEmitter {
  listeners: {[type: string]: EventHandler[] }
  addEventListener(type: EventType, listener: EventHandler, options?: boolean | AddEventListenerOptions): void;
  removeEventListener(type: EventType, listener: EventHandler, options?: boolean | EventListenerOptions): void;
}

export class BaseEvent {
  type: EventType
  eventInit: any

  constructor(type: EventType, eventInit: any) {
    this.type = type
    this.eventInit = eventInit
  }
}

export class DragEvent extends BaseEvent {
  touch: Touch
  identifier: number
  dragging: boolean

  static startWith(touch: Touch) {
    var evt = new DragEvent('dragStart', {})
    evt.identifier = touch.identifier
    evt.touch = touch
    evt.dragging = true
    return evt
  }

  static dragWith(touch: Touch) {
    var evt = new DragEvent('drag', {})
    evt.identifier = touch.identifier
    evt.touch = touch
    evt.dragging = true
    return evt
  }

  static endWith(touch: Touch) {
    var evt = new DragEvent('dragEnd', {})
    evt.identifier = touch.identifier
    evt.touch = touch
    evt.dragging = false
    return evt
  }
}

export class ClickEvent extends BaseEvent {
  touch: Touch
  identifier: number

  static with(touch: Touch) {
    var evt = new ClickEvent('click', {})
    evt.identifier = touch.identifier
    evt.touch = touch
    return evt
  }
}